package com.victor.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.victor.entity.Certificate;
import com.victor.service.CertificateService;
import com.victor.utils.FileUpload;
import com.victor.utils.Msg;

/**
 * 专利证书管理
 * 
 * @author Mr.Zhang
 *
 */
@RestController
@RequestMapping("/certificate")
public class CertificateController {

	@Autowired
	private CertificateService service;
	

	// 获取专利证书列表
	@RequestMapping("/getAll")
	public Msg getAll() {
		List<Certificate> certificates = service.getAll();
		return Msg.success().add("certificates", certificates);
	}

	// 新建专利证书
	@RequestMapping("/build")
	public Msg build(Certificate certificate, MultipartFile c_picture_file, HttpServletRequest request) throws Exception {
		if (c_picture_file != null) {
			String c_picture = FileUpload.uploadFile(c_picture_file, request);
			certificate.setC_picture(c_picture);
		}
		boolean insResult = service.build(certificate);
		return insResult ? Msg.success() : Msg.fail();
	}

	// 修改专利证书信息
	@RequestMapping("/update")
	public Msg update(Certificate certificate, MultipartFile c_picture_file, HttpServletRequest request) throws Exception {
		if (c_picture_file != null) {
			String c_picture = FileUpload.uploadFile(c_picture_file, request);
			certificate.setC_picture(c_picture);
		}
		boolean updResult = service.update(certificate);
		return updResult ? Msg.success() : Msg.fail();
	}

	// 批量删除专利证书
	@RequestMapping("/batchDelete")
	public Msg batchDelete(int[] ids) {
		boolean delResult = service.batchDelete(ids);
		return delResult ? Msg.success() : Msg.fail();
	}
}
